<template>
  <div class="app-container">
    <el-row
      :gutter="10"
      class="mb8"
    >
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="naturalPersonList"
      :span-method="arraySpanMethod"
      border
    >
      <!-- <el-table-column label="指标类别" prop="surveyTarget" :show-overflow-tooltip="true" min-width="150"/> -->
      <el-table-column
        label="指标类别"
        prop="surveyTarget"
        min-width="150"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.config_indicator_type"
            :value="scope.row.surveyTarget"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="指标名称"
        prop="surveyName"
        :show-overflow-tooltip="true"
        min-width="150"
      />
      <el-table-column
        label="计算公式"
        prop="scoreAccord"
        :show-overflow-tooltip="true"
        min-width="150"
      />
      <el-table-column
        label="满分分值"
        prop="score"
        width="150"
      />
      <el-table-column
        label="标识"
        prop="identify"
        width="150"
      />
      <el-table-column
        label="是否代理商"
        prop="isAgent"
        width="220"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.config_isAgent"
            :value="scope.row.isAgent"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改自然人配置对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <!-- <el-form-item label="指标类别" prop="surveyTarget">
                    <el-input v-model="form.surveyTarget" placeholder="请输入指标类别"/>
                </el-form-item> -->
        <el-form-item
          label="指标类别"
          prop="surveyTarget"
        >
          <el-select
            v-model="form.surveyTarget"
            placeholder="请选择指标类别"
            style="width: 100%;"
          >
            <el-option
              v-for="dict in dict.type.config_indicator_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="指标名称"
          prop="surveyName"
        >
          <el-input
            v-model="form.surveyName"
            placeholder="请输入指标名称"
          />
        </el-form-item>
        <el-form-item
          label="计算公式"
          prop="scoreAccord"
        >
          <el-input
            v-model="form.scoreAccord"
            type="textarea"
            placeholder="请输入计算公式"
          />
        </el-form-item>
        <el-form-item
          label="满分分值"
          prop="score"
        >
          <el-input
            v-model="form.score"
            placeholder="请输入满分分值"
          />
        </el-form-item>
        <el-form-item
          label="标识"
          prop="identify"
        >
          <el-input
            v-model="form.identify"
            placeholder="请输入标识"
          />
        </el-form-item>
        <el-form-item
          label="是否代理商"
          prop="isAgent"
        >
          <el-radio-group v-model="form.isAgent">
            <el-radio
              v-for="dict in dict.type.config_isAgent"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="submitForm"
        >确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>
  
<script>
import { listScoreSurvey, addScoreSurvey, updateScoreSurvey, delScoreSurvey } from '@/api/systemConfig/dueDiligence'

export default {
  name: 'legalPerson',
  dicts: ['config_isAgent', 'config_indicator_type'],
  data() {
    return {
      // 遮罩层
      // loading: true,
      loading: false,
      // 总条数
      total: 0,
      // 尽调表格数据
      naturalPersonList: [],
      // 用于记录合并的行数
      mergedRows: {},
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        custType: '02',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        surveyTarget: [
          { required: true, message: '指标类别不能为空', trigger: 'change' }
        ],
        surveyName: [
          { required: true, message: '指标名称不能为空', trigger: 'blur' }
        ],
        scoreAccord: [
          { required: true, message: '计算公式不能为空', trigger: 'blur' }
        ],
        score: [
          { required: true, message: '满分分值不能为空', trigger: 'blur' }
        ],
        identify: [
          { required: true, message: '标识不能为空', trigger: 'blur' }
        ],
        isAgent: [
          { required: true, message: '是否代理商不能为空', trigger: 'change' }
        ],
      }

    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      listScoreSurvey(this.queryParams).then(response => {
        this.naturalPersonList = response.rows
        this.total = response.total
        this.loading = false

        this.mergedRows = {};
        this.naturalPersonList.forEach((item, index) => {
          if (!this.mergedRows[item.surveyTarget]) {
            this.mergedRows[item.surveyTarget] = {
              rowIndex: index,
              rowspan: 1
            };
            let count = 1;
            for (let i = index + 1; i < this.naturalPersonList.length; i++) {
              if (this.naturalPersonList[i].surveyTarget === item.surveyTarget) {
                count++;
              } else {
                break;
              }
            }
            this.mergedRows[item.surveyTarget].rowspan = count;
          }
        });
      }
      )
    },

    // span-method表格合并
    arraySpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        if (this.mergedRows[row.surveyTarget]) {
          if (rowIndex === this.mergedRows[row.surveyTarget].rowIndex) {
            return {
              rowspan: this.mergedRows[row.surveyTarget].rowspan,
              colspan: 1
            };
          } else {
            return {
              rowspan: 0,
              colspan: 0
            };
          }
        }
      }
    },

    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        surveyTarget: undefined,
        surveyName: undefined,
        scoreAccord: undefined,
        score: undefined,
        identify: undefined,
        isAgent: undefined,
      }
      this.resetForm('form')
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加配置'
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.form.id = row.id
      this.form.surveyTarget = row.surveyTarget
      this.form.surveyName = row.surveyName
      this.form.scoreAccord = row.scoreAccord
      this.form.score = row.score
      this.form.identify = row.identify
      this.form.isAgent = row.isAgent
      this.open = true
      this.title = '修改配置'
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          let params = {
            ...this.form,
            custType: '02'
          }
          if (this.form.id != undefined) {
            updateScoreSurvey(params).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addScoreSurvey(params).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除该数据项？').then(function () {
        return delScoreSurvey(row.id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },

  }
}
</script>